//
//  MNDDepositBankCardViewController.m
//  minidai
//
//  Created by 容贤郁 on 2018/3/21.
//  Copyright © 2018年 迷你金服. All rights reserved.
//

#import "MNDDepositBankCardViewController.h"
#import "MNDBankModel.h"
#import "NSString+ReplaceAsterisk.h"
#import "MNDBankCardViewController.h"
#import "NSString+BankCardFormatter.h"
#import "MNDBindBankcardViewController.h"
#import "MNDDepositChangePhoneViewController.h"
#import "MNDDepositWebViewController.h"
#import "MNDOpenDepositoryPaymentViewController.h"
#import "MNDOpenDepositoryTenderViewController.h"
#import "MNDOpenDepositoryTransferViewController.h"

@interface MNDDepositBankCardViewController ()

/**
 存管银行logo
 */
@property (weak, nonatomic) IBOutlet UIImageView *depositBankLogo;

/**
 存管银行名称
 */
@property (weak, nonatomic) IBOutlet UILabel *depositBankNameLabel;

/**
 存管银行开户人姓名
 */
@property (weak, nonatomic) IBOutlet UILabel *depositUsernameLabel;

/**
 电子银行卡号
 */
@property (weak, nonatomic) IBOutlet UILabel *depositCardNoLabel;

/**
 存管银行
 */
@property (weak, nonatomic) IBOutlet UILabel *depositNameLabel;

@property (weak, nonatomic) IBOutlet UIImageView *depositBankBackground;

@property (weak, nonatomic) IBOutlet UIButton *pasteboardButton;

/**
 开户人到电子账户之间的间距
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *nameToCarNoConstraint;

/**
 存管银行名称到电子账户之间的间距
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bankToCardNoConstraint;

@property (strong, nonatomic) MNDBankModel *bankModel;

@property (weak, nonatomic) IBOutlet UILabel *bindCardNoLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *paymentPasswordLabel;

/**
 是否投资授权
 */
@property (nonatomic, assign) BOOL isInvestAuth;

@end

@implementation MNDDepositBankCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.depositBankBackground.layer.cornerRadius = 5.0;
    self.depositBankBackground.layer.shadowOpacity = 0.4;
    self.depositBankBackground.layer.shadowOffset = CGSizeMake(5.0, 5.0);
    self.depositBankBackground.layer.shadowRadius = 5.0;
//    self.depositBankBackground.layer.masksToBounds = YES;
    self.depositBankBackground.layer.shadowColor = kColorBankGreen.CGColor;
    
    if (kScreenWidth <= 320) {
        self.nameToCarNoConstraint.constant = 0.0;
        self.bankToCardNoConstraint.constant = 0.0;
    }
    
    if (kScreenWidth <= 320) {
        [self.depositBankBackground setImage:[UIImage imageNamed:@"bank_background_640"]];
    }
    
    self.tableView.tableFooterView = [UIView new];
    
    [NetworkProgressHUD showHud];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadBankInfo];
}

#pragma mark - Network

- (void)loadBankInfo {
    
    WeakSelf(self);
    
    [APIManager sendRequestWithPath:API_GET_BANK_ACCOUNT
                         parameters:[MNDSign signedParam]
                             method:kXMHTTPMethodGET
                          onSuccess:^(ResponseModel *responseModel) {
        
                              if (responseModel.status == ResponseSuccess) {
                                  [NetworkProgressHUD hideHud];
                                  
                                  [weakself configBankInfo:responseModel.data];
                              } else {
                                  [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
                              }
                          } onFailure:^(NSError *error) {
                              [NetworkProgressHUD showHudNetworkError:error];
                          }];
}

- (void)configBankInfo:(NSDictionary *)dict {
    self.bankModel = [MNDBankModel mj_objectWithKeyValues:dict];
    
    self.depositCardNoLabel.text = [self.bankModel.jxBankaccount.userAmountId formatterBankCardNum];
    self.depositUsernameLabel.text = self.bankModel.userAmount.userRealname;
    
    if (self.bankModel.jxBankaccount.userAmountCardno.length > 0) {
        self.bindCardNoLabel.text = [NSString stringWithFormat:@"%@%@", self.bankModel.jxBankaccount.bank.bank, self.bankModel.jxBankaccount.lastNumber];
        self.bindCardNoLabel.textColor = kColor9;
    } else {
        self.bindCardNoLabel.text = @"未绑定";
        self.bindCardNoLabel.textColor = kColorDarkOrange;
    }
    
    self.phoneLabel.text = [self.bankModel.jxBankaccount.userAmountMobile replaceWithAsteriskAtRange:NSMakeRange(3, 4)];
    if (self.bankModel.jxBankaccount.userAmountIssetspas.integerValue == 1) {
        self.paymentPasswordLabel.textColor = kColor9;
        self.paymentPasswordLabel.text = @"修改";
    } else {
        self.paymentPasswordLabel.textColor = kColorDarkOrange;
        self.paymentPasswordLabel.text = @"去设置";
    }
    
    JxBankaccount *bankAccount = self.bankModel.jxBankaccount;
    
    self.isInvestAuth = bankAccount.userAmountIssetspas.integerValue == 1 &&
                        bankAccount.userAmountIspayment.integerValue == 1 &&
                        bankAccount.userAmountIsatender.integerValue == 1 &&
                        bankAccount.userAmountIsacretransfer.integerValue == 1;
    
    [self.tableView reloadData];
}

- (IBAction)pasteboardAction:(id)sender {
    //复制内容
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = self.depositCardNoLabel.text;
    [NetworkProgressHUD showHudMsg:@"银行卡号已复制到剪切板"];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10;
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    view.tintColor = kColorF5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (self.isInvestAuth) {
        return 3;
    } else {
        return 4;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //绑定银行卡或者查看银行卡
    if (indexPath.row == 0) {
        if (self.bankModel.jxBankaccount.userAmountCardno.length > 0) {
            MNDBankCardViewController *bankcardVC = [UIStoryboard viewControllerWithIdentifier:[MNDBankCardViewController description]];
            bankcardVC.bankModel = self.bankModel;
            
            [self.navigationController pushViewController:bankcardVC animated:YES];
        } else {
            
            [self loadBindingCardPage];
//            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
//            MNDBindBankcardViewController *bindCardVC = [storyboard instantiateViewControllerWithIdentifier:[MNDBindBankcardViewController description]];
//            bindCardVC.bankModel = self.bankModel;
//            
//            [self.navigationController pushViewController:bindCardVC animated:YES];
        }
    }
    //修改开户手机号
    if (indexPath.row == 1) {
        
        MNDDepositChangePhoneViewController *changePhoneVC = [UIStoryboard viewControllerWithIdentifier:[MNDDepositChangePhoneViewController description]];
        changePhoneVC.phoneNum = self.bankModel.jxBankaccount.userAmountMobile;
        
        [self.navigationController pushViewController:changePhoneVC animated:YES];
    }
    //修改或设置交易密码
    if (indexPath.row == 2) {
//        if (self.bankModel.jxBankaccount.userAmountIssetspas.integerValue == 1) {
//
//            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
//            UINavigationController *naviVC = [storyboard instantiateViewControllerWithIdentifier:@"ResetPassworddNaviVC"];
//
//            [self presentViewController:naviVC animated:YES completion:^{
//
//            }];
//        } else {
            [self loadPaymentPassword];
//        }
    }
    //完成其他步骤的投资授权
    if (indexPath.row == 3) {
        if (self.bankModel.jxBankaccount.userAmountIssetspas.integerValue == -1) {
            [self loadPaymentPassword];
            
            return;
        }
        [self toOpenDepository];
    }
}

- (void)loadBindingCardPage {
    
    [NetworkProgressHUD showHud];
    WeakSelf(self);
    
    [APIManager sendRequestWithPath:API_BINDING_BANKCARD parameters:[MNDSign signedParamWithDict:@{@"channel": @"app"}] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD hideHud];
            
            MNDDepositWebViewController *webVC = [UIStoryboard viewControllerWithIdentifier:[MNDDepositWebViewController description]];
            webVC.htmlStr = responseModel.data;
            webVC.title = @"绑定银行卡";
//            webVC.shouldBack = YES;
            NSInteger index = [weakself.navigationController.viewControllers indexOfObject:weakself];
            webVC.fromNaviIndex = index;
            
            [weakself.navigationController pushViewController:webVC animated:YES];
        } else {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
        
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

/**
 设置交易密码
 */
- (void)loadPaymentPassword {
    
    [NetworkProgressHUD showHud];
    
    WeakSelf(self);
    
    [APIManager sendRequestWithPath:API_DEPOSIT_SET_PASSWORD parameters:[MNDSign signedParamWithDict:@{@"channel": @"app"}] method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD hideHud];
            MNDDepositWebViewController *webVC = [UIStoryboard viewControllerWithIdentifier:[MNDDepositWebViewController description]];
            webVC.htmlStr = responseModel.data;
            webVC.title = @"设置交易密码";
            webVC.shouldBack = YES;
            NSInteger index = [weakself.navigationController.viewControllers indexOfObject:weakself];
            webVC.fromNaviIndex = index;
            
            [weakself.navigationController pushViewController:webVC animated:YES];
        } else {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (void)toOpenDepository {
    
    NSInteger index = [self.navigationController.viewControllers indexOfObject:self];
    
    //设置缴费授权
    if (self.bankModel.jxBankaccount.userAmountIspayment.integerValue == -1) {
        
        MNDOpenDepositoryPaymentViewController *paymentVC = [UIStoryboard viewControllerWithIdentifier:[MNDOpenDepositoryPaymentViewController description]];
        paymentVC.fromNaviIndex = index;
        
        [self.navigationController pushViewController:paymentVC animated:YES];
        
        return;
    }
    //设置自动投标
    if (self.bankModel.jxBankaccount.userAmountIsatender.integerValue == -1) {
        
        MNDOpenDepositoryTenderViewController *tenderVC = [UIStoryboard viewControllerWithIdentifier:[MNDOpenDepositoryTenderViewController description]];

        tenderVC.fromNaviIndex = index;
        tenderVC.phoneNumber = self.bankModel.jxBankaccount.userAmountMobile;
            
        [self.navigationController pushViewController:tenderVC animated:YES];

        return;
    }
    //设置自动债转
    if (self.bankModel.jxBankaccount.userAmountIsacretransfer.integerValue == -1) {
        
        MNDOpenDepositoryTransferViewController *transferVC = [UIStoryboard viewControllerWithIdentifier:[MNDOpenDepositoryTransferViewController description]];
        transferVC.fromNaviIndex = index;
        transferVC.phoneNumber = self.bankModel.jxBankaccount.userAmountMobile;
            
        [self.navigationController pushViewController:transferVC animated:YES];
        
        return;
    }
}




@end
